#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    using ULL = unsigned long long;
    unordered_map<int, ULL> minNum;
    ULL res = 0;
    void dfs(int level, TreeNode* root, ULL set)
    {
        if (!root)
            return;
        if (!minNum[level])
            minNum[level] = set;
        res = max(set - minNum[level] + 1, res);
        dfs(level + 1, root->left, set * 2);
        dfs(level + 1, root->right, set * 2 + 1);
    }
    int widthOfBinaryTree(TreeNode* root) {
        if (!root)
            return 0;
        dfs(0, root, 1);
        return res;
    }
};